package com.yeyks.commonReference.service.other.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.appcommon.dto.platform.AppActivityInfoDTO;
import com.yeyks.common.appcommon.param.platform.QueryActivityParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.dto.ActivityInfoDTO;
import com.yeyks.common.dto.mq.WebToCSendMessage;
import com.yeyks.common.em.ResponseStatus;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.append.rocketmq.service.producer.MQProducerService;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.other.ActivityInfoService;
import com.yeyks.other.dal.dao.ActivityInfoMapper;
import com.yeyks.other.dal.domain.ActivityInfo;
import com.yeyks.other.model.AddActivityInfoParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 活动通知表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
@Slf4j
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {

    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private MQProducerService mqProducerService;

    @Override
    public ResponseEntity<AjaxResponseBody> list(String token, Integer pageNum, Integer pageSize) {

        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {

            AccountInfo operatorInfo = accountService.token2AccountInfo(token);
            log.info("当前操作人" + operatorInfo.getName());

            Map<String, Object> resultMap = new HashMap<>();
            int count = activityInfoMapper.selectCount(null);
            List<ActivityInfo> activityInfoList = activityInfoMapper.selectPage2((pageNum-1)*pageSize, pageSize);
            List<ActivityInfoDTO> activityInfoDTOList = new ArrayList<ActivityInfoDTO>();
            if(!CollectionUtils.isEmpty(activityInfoList)){
                activityInfoDTOList = ObjectConvertUtil.convertList(activityInfoList,ActivityInfoDTO.class);
            }
            resultMap.put("count",count);
            resultMap.put("list", activityInfoDTOList);
            responseBody.setBody(resultMap);
            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());

        } catch (Throwable e){
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        return ResponseEntity.ok(responseBody);
    }

    @Transactional
    @Override
    public ResponseEntity<AjaxResponseBody> add(AddActivityInfoParam addActivityInfoParam) {

        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {

            if(null != addActivityInfoParam){

                AccountInfo operatorInfo = accountService.token2AccountInfo(addActivityInfoParam.getToken());
                log.info("当前操作人" + operatorInfo.getName());

                ActivityInfo activityInfo = ObjectConvertUtil.convertObject(addActivityInfoParam, ActivityInfo.class);
                //1默认为可用
                activityInfo.setStatus(1);
                activityInfo.setOperator(operatorInfo.getId());
                int count = activityInfoMapper.insertSelective(activityInfo);
                if(count > 0){
                    //发送消息，通过mq
                    mqProducerService.webToCSendMsg(new WebToCSendMessage(1, String.valueOf(activityInfo.getId())));
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                }
            }else{
                responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
                responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
            }
        } catch (Throwable e){
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 通过活动id获取活动详情
     *
     * @param id
     * @return
     */
    @Override
    public ActivityInfo info(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public CommonResult<CommonPage<AppActivityInfoDTO>> selectPageByParam(QueryActivityParam param, AccountInfo accountInfo) {

        CommonPage<AppActivityInfoDTO> activityInfoDTOCommonPage = new CommonPage<>();

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<ActivityInfo> activityInfos = baseMapper.selectList(null);
        if(!CollectionUtils.isEmpty(activityInfos)){
            CommonPage<ActivityInfo> activityInfoCommonPage = CommonPage.restPage(param.getPageNum(), param.getLastId(), activityInfos);
            activityInfoDTOCommonPage = MyBeanUtils.convertCommonPageList(activityInfoCommonPage, AppActivityInfoDTO.class);
        }
        return CommonResult.success(activityInfoDTOCommonPage);
    }

}
